package cn.itcast.bofe.action;

import cn.itcast.bofe.utils.BaseAction;
import cn.itcast.bofe.utils.MailUtils;
import cn.itcast.crm.domain.Customer;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer>{
    @Autowired
    private RedisTemplate<String ,String> redisTemplate;

    @Autowired
    @Qualifier("jmsQueueTemplate")
    private JmsTemplate jmsTemplate;

    @Action(value = "customer_sendSms")
    public String senSms(){
        String randomCode = RandomStringUtils.randomNumeric(4);
        ServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(),randomCode);

        System.out.println("手机验证码为:"+randomCode);
        final String msg = "尊敬的客户,本次您的验证码为:" + randomCode ;
        /*String result = "000/xxx";
        if (result.startsWith("000")){
            return NONE;
        }else {
            throw new RuntimeException("短信发送失败,信息码:"+result);
        }*/
        jmsTemplate.send("bos_sms", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                MapMessage mapMessage = session.createMapMessage();
                mapMessage.setString("telephone",model.getTelephone());
                mapMessage.setString("msg",msg);
                return mapMessage;
            }
        });
        return NONE;
    }

    private String checkcode;

    public void setCheckcode(String checkcode) {
        this.checkcode = checkcode;
    }

    @Action(value = "customer_regist",results = {
            @Result(name = SUCCESS,type = "redirect",location = "signup-success.html"),
            @Result(name = INPUT,type = "redirect",location = "signup.html")
    })
    public String regist(){
        String checkcodeSession = (String) ServletActionContext.getRequest().getSession().getAttribute(model.getTelephone());

        System.out.println(model.getTelephone());
        if (checkcodeSession == null || !checkcodeSession.equals(checkcode)){
            System.out.println("短信验证码错误!");
            return INPUT;
        }
        WebClient.create("http://localhost:9002/services/customerService/customer")
                .type(MediaType.APPLICATION_JSON).post(model);
        //System.out.println("恭喜你,注册成功!");

        String activecode = RandomStringUtils.randomNumeric(32);
        redisTemplate.opsForValue().set(model.getTelephone(),activecode,24,TimeUnit.HOURS);
        String content = "尊敬的用户,请24小时内点击下面地址进行激活账户:<br/><a " +
                "href='"+MailUtils.activeUrl +"?telephone="+model.getTelephone()+"&activecode="+activecode+"'>点击我</a>";
        MailUtils.sendMail("速运快递激活邮件",content,model.getEmail());
        return  SUCCESS;
    }

    private String activecode;


    public void setActivecode(String activecode) {
        this.activecode = activecode;
    }
    @Action(value = "customer_activeMail")
    public String activeMail() throws IOException {
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        System.out.println(activecode);
        System.out.println(model.getTelephone());
        String activecodeRedis = redisTemplate.opsForValue().get(model.getTelephone());
        System.out.println(activecodeRedis);
        if (activecodeRedis == null || !activecodeRedis.equals(activecode)){
            ServletActionContext.getResponse().getWriter().println("激活无效,请重新激活!");
        }else {
            ServletActionContext.getResponse().getWriter().println("激活成功!");
        }
        return NONE;
    }
}
